using MediatR;
using Movies.Application.DTOs;
using Movies.Application.Extensions;
using Movies.Application.Mappers;
using Movies.Application.Queries;
using Movies.Application.Responses;
using Movies.Core.Entities;
using Movies.Core.Repositories;

namespace Movies.Application.Handlers;

public class GetMoviesByMovieNameHandler : IRequestHandler<GetMoviesByMovieNameQuery, GetMovieDto?>
{
    private readonly IMovieRepository _movieRepository;

    public GetMoviesByMovieNameHandler(IMovieRepository movieRepository)
    {
        _movieRepository = movieRepository;
    }

    public async Task<GetMovieDto?> Handle(
        GetMoviesByMovieNameQuery request,
        CancellationToken cancellationToken
    )
    {
        Movie? movie = await _movieRepository.GetMoviesByMovieName(request.MovieName);
        if (movie == null)
        {
            return null;
        }
        GetMovieDto getMovieDto = movie.MapTo();
        return getMovieDto;
    }
}
